<div class="layui-body-header">
	<h2>行业列表</h2>
</div>

<form action="" class="layui-form">	
	<div class="layui-card-body">
		<div class="layui-fluid">
			<div class="layui-card">
		      <div class="layui-card-header">
		        <!-- <span>所有会员列表</span> -->
		        <a href="#/layui/addHangYe" class="layui-btn layui-btn-sm">
		          <i class="layui-icon">&#xe654;</i>
		          <span>添加</span>
		        </a>
		        <button type="button" class="layui-btn layui-btn-sm  layui-btn-primary edit">
		          <i class="layui-icon">&#xe640;</i>
		          <span>编辑</span>
		        </button>
		        <button type="button" class="layui-btn layui-btn-sm layui-btn-danger del" lay-event="delete">
		          <i class="layui-icon">&#xe640;</i>
		          <span>删除</span>
		        </button>
		        <!--<a href="javascript:;" class="layui-btn layui-btn-xs layui-btn-primary" id="search_hash">
		          <i class="layui-icon">&#xe615;</i>
		        </a>-->
		      </div>
		      <!--<div class="friHang-warp">
		      	<div class="warp">
		      		第一行业：
		      	</div>
		      	<div class="warp">
		      		<select name="friHang" class="friHang" lay-filter="fri_hang"> 
				        <option value="">一级行业</option>
				      </select>
		      	</div>
		
			  </div>
			  <div class="secHange-warp">
			  	<div class="warp">
			  		第二行业：
			  	</div>
			  	<div class="warp">
			  		<select name="friHang" class="secHang" lay-filter="sec_hang">
				        <option value="">二级行业</option>
				      </select>
			  	</div>
			      
			  </div>-->
			  
			  <div class="hangye" >
			  	
			  </div>
			  
			  
			    </div>
			</div>
	      </div>
      
      </form>
      
      




<script src="../../api/api.js" type="text/javascript" charset="utf-8"></script>

<script>
	let checkedArr = [];
//	let batchDel = document.querySelector(".batchDel");
	let del = document.querySelector(".del");
//	选中当前行的信息数组
	let objArr = [];
	let friHang = document.querySelector(".friHang");
	let secHang = document.querySelector(".secHang");
	let friHangId = [];
	let secHangId = [];
	let edit = document.querySelector(".edit");
	let hangye = document.querySelector(".hangye");
	let isOneArr = [];
	let isTwoArr = []
	let isDel = false;
	let secDom = "";
	let friDom = "";
	
  layui.config({
    base: '/src/js/'
  }).use(['jquery', 'mockjs', 'table', 'sidebar', 'form'], function () {
    var $ = layui.jquery,
      layer = layui.layer,
      table = layui.table,
      sidebar = layui.sidebar,
      form = layui.form;
      
      renderForm();
      
      $.ajax({//异步请求返回给后台
    	  url:"https://xnsmb.xnsoft.net.cn/api/GetXn_Trade?FatherId=00",
    	  type:'get',
    	  data:{},
    	  dataType:'json',
    	  beforeSend: function(request) {
//          loadingImg()
            request.setRequestHeader("Authorization", 'bearer ' + localStorage.getItem("token"));
        },
    	  id:'test',
    	  success:function(data){
//  	  	$(".friHang").empty();
//  	  	console.log(friHang)
//  		  let p = document.createElement('option');	
//  		  p.value='';
//  		  p.innerText="一级行业";
//  		  friHang.appendChild(p)
//  		  data.forEach((el,index)=>{
//  		  	let op = document.createElement('option');
//  		  		op.id = el.Id;
//					op.value = el.Code;
//	    		  	op.innerText = el.Name;
//	    		  	friHang.appendChild(op)
//  		  })

			  data.forEach((el,index)=>{
//			  	console.log(el);
			  	hangye.innerHTML += `<div class="${el.Code}">
			  							<input type="checkbox" lay-filter="jobsOne" lay-skin="primary" value="${el.Code}" id="${el.Id}" name="" title="${el.Name}">
			  							<div class="${el.Name}">
			  							
			  							</div>
			  						</div> `
			  	
			  })
    		  renderForm();
    		  form.on("checkbox(jobsOne)",function(data){
    		  	
    		  	let friId = '';
    		  	let parent = data.elem.parentNode;
    		  	let checkOne = [];
    		  	let son = document.createElement('p');
    		  	console.log(data.elem.parentNode.classList[0])
    		  		if(data.elem.checked){
    		  			friId = data.elem.getAttribute('id');
    		  			friHangId.push(friId)
    		  			console.log("friHangId",friHangId)
    		  			$.ajax({//异步请求返回给后台
				    	  url:"https://xnsmb.xnsoft.net.cn/api/GetXn_Trade",
				    	  type:'get',
				    	  data:{FatherId:data.value},
				    	  dataType:'json',
				    	  beforeSend: function(request) {
					//          loadingImg()
					            request.setRequestHeader("Authorization", 'bearer ' + localStorage.getItem("token"));
					        },
				    	  success:function(data){
				    	  	let checkOneArr = [];
				    	  	
				    	  	son.classList.add("son");
				    	  	
				    	  	checkOneArr.push(data.Id)
				    	  	
				    	  	console.log(data);
				    	  	
				    	  	data.forEach((el,index)=>{
				    	  		son.innerHTML += `<div>····· <input type="checkbox" class="sec" lay-filter="jobsTwo" lay-skin="primary" id="${el.Id}" value="${el.Code}" name="" title="${el.Name}"></div> `
				    	  	})
				    	  	parent.appendChild(son)
//				    	  	$(".secHang").empty();
//				    	  	console.log(data)
//				    		  let p = document.createElement('option');	
//				    		  p.value='';
//				    		  p.innerText="二级行业";
//				    		  secHang.appendChild(p)
//				    		  data.forEach((el,index)=>{
//				    		  	let op = document.createElement('option');
//									op.value = el.Id;
//					    		  	op.innerText = el.Name;
//					    		  	secHang.appendChild(op)
//				    		  })
				    		  renderForm();
				    		  
				    		  form.on('checkbox(jobsTwo)',function(data){
//				    		  	console.log(data.elem.parentNode.parentNode.parentNode.children[0].id)
				    		  	let secId = '';
				    		  	let fatherId = data.elem.parentNode.parentNode.parentNode.children[0].id;
				    		  	
				    		  	if(data.elem.checked){
//				    		  		console.log(friHangId.indexOf(fatherId),999)
				    		  		if(friHangId.indexOf(fatherId)!=-1){
				    		  			friHangId.splice(friHangId.indexOf(fatherId),1)
				    		  		}
				    		  		
				    		  		console.log(555,friHangId)
//				    		  		console.log(data.elem.getAttribute('id'))
							      	secId = data.elem.getAttribute('id')
							      	secHangId.push(secId);
							      	checkOne.push(secId)
							      	console.log("checkOne",checkOne)
//							      	console.log("secHangId",secHangId)
				    		  	}else{
//				    		  		console.log("data.elem.getAttribute('id')",data.elem.getAttribute('id'))
				    		  		secHangId.splice(secHangId.indexOf(data.elem.getAttribute('id')), 1);
				    		  		checkOne.splice(checkOne.indexOf(data.elem.getAttribute('id')),1);
				    		  		console.log("checkOne",checkOne)
				    		  		if(checkOne.length==0){
				    		  			console.log(friId,"friId")
//				    		  			friHangId.splice(friHangId.indexOf(friId),1)
										friHangId.push(fatherId)
				    		  			console.log("friHangId",friHangId)
				    		  		}
//				    		  		console.log(secHangId)
				    		  	}
				    		  	
						      	
						      })
				    		  
				    	  }
				    	  
				      });
    		  		}else {
//  		  			debugger
    		  			console.log(222,friHangId.indexOf(data.elem.getAttribute('id')))
    		  			friHangId.splice(friHangId.indexOf(data.elem.getAttribute('id')),1);
//  		  			console.log("friHangId",friHangId)
    		  			parent.removeChild(parent.lastChild)
//  		  			renderForm();
    		  		}
    		  })
    		  
    		  
    	  }
    	  
      });
      
      
      
      form.on('select(fri_hang)', function(data){
//    	console.log("dataId",data.elem[data.elem.selectedIndex].id)
			friDom = data.elem[data.elem.selectedIndex];
      		friHangId = data.elem[data.elem.selectedIndex].id;
      		
      		console.log(999,friHangId)
	      	$.ajax({//异步请求返回给后台
	    	  url:"https://xnsmb.xnsoft.net.cn/api/GetXn_Trade",
	    	  type:'get',
	    	  data:{FatherId:data.value},
	    	  dataType:'json',
	    	  beforeSend: function(request) {
	//          loadingImg()
	            request.setRequestHeader("Authorization", 'bearer ' + localStorage.getItem("token"));
	        },
	    	  success:function(data){
	    	  	$(".secHang").empty();
	    	  	console.log(data)
	    		  let p = document.createElement('option');	
	    		  p.value='';
	    		  p.innerText="二级行业";
	    		  secHang.appendChild(p)
	    		  data.forEach((el,index)=>{
	    		  	let op = document.createElement('option');
						op.value = el.Id;
		    		  	op.innerText = el.Name;
		    		  	secHang.appendChild(op)
	    		  })
	    		  renderForm();
	    	  }
	    	  
	      });
      })
      
      form.on('select(sec_hang)', function(data){
      	secDom = data.elem[data.elem.selectedIndex];
      	console.log("secDom",secDom)
      	secHangId = data.value;
      	console.log("secHangId",secHangId)
      })
      
      

    $('#search_hash').on('click', function () {
      var that = this;
      // console.log($('#search_tpl_hash').html());
      sidebar.render({
        elem: that,
        content: $('#search_tpl_hash').html(),
        title: '搜索',
        shade: true,
        // shadeClose:false,
        // direction: 'left'
        // dynamicRender: false,
        // url: 'views/member/all/search.html',
        width: '500px', //可以设置百分比和px
        done: function () {
          console.log('ccc');
          form.render();

          //监听提交
          form.on('submit(formDemo_hash)', function (data) {
            // layer.msg(JSON.stringify(data.field));
            table.reload('demo_hash', {
              where: data.field
            });
            return false;
          });
        }
      });
    });
    
    table.on('tool(test)',function(obj){
    	toolHandel("#/layui/addLev?id=","Admin_Xn_Rank/",obj);
    })
    
    
    
//  批量删除
//  batchDel.addEventListener("click",function(){
//  	batDel("Admin_Xn_Rank/",checkedArr,objArr)
//  	
//  })
    
    
    function renderForm(){
		  layui.use('form', function(){
		   var form = layui.form;//高版本建议把括号去掉，有的低版本，需要加()
		   form.render();
		  });
		}
    
    
    edit.addEventListener("click",function(){
    	
//  	if(secHangId){
//  		console.log("Hi")
//  		window.location.href="#/layui/addHangYe?id="+secHangId;
//  	}else if(friHangId){
//  		window.location.href="#/layui/addHangYe?id="+friHangId;
//  	}
		if(secHangId.length>1){
			layer.confirm('请不要选择超过两个选项')
//			console.log("请不要选择超过两个选项")
		}else if(secHangId.length==1){
			window.location.href="#/layui/addHangYe?id="+secHangId[0];
		}else if(friHangId.length>1){
			layer.confirm('请不要选择超过两个选项')
//			console.log("请不要选择超过两个选项")
		}else if(friHangId.length==1){
			window.location.href="#/layui/addHangYe?id="+friHangId[0];
		}
    })
    
//  del.addEventListener("click",function(){
//  	if(secHangId||friHangId){
//  		layer.confirm('确认删除？', function(){
//  			if(secHangId){
////  				console.log(secHangId)
//  				secDom.parentNode.removeChild(secDom)
//  				
//  				DBHelper.Delete("Admin_Xn_Trade?id="+secHangId,function(data){
//  					console.log(data)
//  					console.log(secDom)
//  				},function(data){
//  					console.log(data)
//  				})
//					renderForm();
//  			}else{
////  				console.log(friHangId)
//					friDom.parentNode.removeChild(friDom)
//  				DBHelper.Delete("Admin_Xn_Trade?id="+friHangId,function(data){
//  					console.log(data)
//  					
//  				},function(data){
//  					console.log(data)
//  				})
//  				renderForm();
//  			}
//	    		layer.close(layer.index);
//	    		renderForm();
//	    	})
//  	}
//  	
//  })


//  if(layEvent==="delete"){}
    
    del.addEventListener("click",function(){
    		let checkedArr = [...friHangId,...secHangId];
    		let isDel = true;
    		
    		console.log(111,friHangId)
    		if(checkedArr.length!=0){
	    		friHangId.forEach((el,index)=>{
	    			console.log("el",$("#"+el+"").parent().children(".son").children().length)
	    			if($("#"+el+"").parent().children(".son").children().length!=0){
	    				isDel = false;
	    				console.log("isDel222",isDel)
	    			}
	    		})
	    		console.log("isDel",isDel)
	    		
	    		if(isDel){
		    				let a = layer.confirm('确认删除？', function(index){
								layer.close(a);	
								console.log("checkedArr",checkedArr)
								DBHelper.Delete("Admin_Xn_Trade",checkedArr,function(data){
							      	console.log(data)
							      },function(data){
			//				      	table.reload('test', {
			//								});
			//						debugger
									location.href = window.location.href
							      	console.log(data)
							      })
								
					    });
	    		}else{
	    			let index = layer.alert("请不要删除拥有二级分类的一级分类",function(){
	    				layer.close(index)
	    			})
	    		}
    		}
    		
//	    	if(checkedArr.length!=0){
//	    		layer.confirm('确认删除？', function(index){
//						layer.close(index);	
//						console.log("checkedArr",checkedArr)
//						DBHelper.Delete("Admin_Xn_Trade",checkedArr,function(data){
//					      	console.log(data)
//					      },function(data){
//	//				      	table.reload('test', {
//	//								});
//	//						debugger
//							location.href = window.location.href
//					      	console.log(data)
//					      })
//						
//			    });
//  	}
	
			
			
    })
    
    
  });
</script>




<style scoped>
	.hangye{
		margin-left: 20px;
		margin-top: 20px;
	}
	.sec{
		margin-left: 20px;
	}
		.friHang-warp{
			margin-top: 20px;
			margin-left: 10px;
		}
		.secHange-warp{
			margin-top: 20px;
			margin-left: 10px;
			padding-bottom: 20px;
		}
		.warp{
			display: inline-block;
		}
      	.title{
      		margin-left: 30px;
      		margin-top: 30px;
      	}
      	.memLevBtn{
      		margin-left: 15px;
      		margin-top: 10px;
      		
      	}
      	.memLevBtn button{
      		border-radius: 5px;
      	}
      	
      </style>